package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加套餐信息
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //添加套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        log.info("套餐菜品添加:{}",setmeal);
        setmealMapper.insert(setmeal);
        //获取添加后套餐id
        Long setmealId = setmeal.getId();

        //添加套餐关系信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes!=null && !setmealDishes.isEmpty()){
            //建立套餐关系id
            log.info("套餐与菜品建立关系的id:{}",setmealId);
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            //批量插入套餐关系信息
            setmealDishMapper.insertBatch(setmealDishes);
        }



    }

    /**
     * 套餐分页查询  --->  套餐名称查询 与 套餐分类id查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页插件
        Page<Object> objects = PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //总页数
        long total = objects.getTotal();
        //查询符合套餐分页数据
        List<Setmeal> pages = setmealDishMapper.queryPage(setmealPageQueryDTO);

        return new PageResult(total,pages);
    }

    /**
     * 套餐批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否起售 起售不能删除
        // todo 能否用一句sql 搞定 (待优化)
        List<Setmeal> list =setmealMapper.queryByIds(ids);
        list.forEach(setmeal->{
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //删除套餐基本信息
        setmealMapper.deleteBatch(ids);

        //删除套餐相关菜品信息
        setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 通过id 查找 套餐基本信息 与 套餐菜品信息
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDish(Long id) {

        //套餐基本信息
        Setmeal setmeal = setmealMapper.queryById(id);

        //套餐菜品信息
        List<SetmealDish> lists = setmealDishMapper.getBySetmealId(id);


        SetmealVO setmealVO = new SetmealVO();

        //套餐基本信息封装
        BeanUtils.copyProperties(setmeal,setmealVO);

        //套餐菜品信息封装
        setmealVO.setSetmealDishes(lists);

        return setmealVO;
    }

    /**
     * 起售 停售 修改
     * @param id
     * @param status
     */
    public void startOrStop(Long id, Integer status) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        //修改id的起售与停售状态
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id 查找套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {

        List<Setmeal> categoryIds = setmealMapper.queryByCategoryId(categoryId);
        return categoryIds;
    }

    /**
     * 通过套餐ID查询 套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal getById(Long id) {
        return setmealMapper.selectById(id);
    }
}
